package com.caigou.comm.base.interceptor

import com.alibaba.android.arouter.utils.ClassUtils
import com.caigou.comm.base.container.BaseApplication

/**
 * @author #Suyghur.
 * Created on 2021/09/29
 */
object ReflectionUtils {

    private val classList = mutableListOf<String>()

    fun getRequireLoginPage(): List<String> {
        kotlin.runCatching {
            val clzList = ClassUtils.getFileNameByPackageName(BaseApplication.getContext(), "com.android.processor.apt")
            for (name in clzList) {
                if (!name.endsWith("RequireLogin")) {
                    continue
                }
                val clz = Class.forName(name)
                val method = clz.getDeclaredMethod("getRequireLoginList")
                method.isAccessible = true
                val list = method.invoke(null) as List<String>
                if (list.isEmpty()) {
                    continue
                }
                classList.addAll(list)
            }
            return classList
        }.onFailure {
            return emptyList()
        }
        return emptyList()
    }

    fun getLoginStatus(): Boolean {
        kotlin.runCatching {
            val clzList = ClassUtils.getFileNameByPackageName(BaseApplication.getContext(), "com.android.processor.apt")
            for (name in clzList) {
                if (!name.endsWith("InjectLogin")) {
                    continue
                }
                val clz = Class.forName(name)
                val method = clz.getDeclaredMethod("getInjectLoginClass")
                method.isAccessible = true
                val targetClzString = method.invoke(null) as String
                val targetClz = Class.forName(targetClzString)
                val field = targetClz.getDeclaredField("isLogin")
                field.isAccessible = true
                return field.getBoolean(targetClz)
            }
            return false
        }.onFailure {
            return false
        }
        return false
    }
}